package app.netant.galgame.yuinya.net;

import org.jsoup.select.Elements;

import app.netant.NetAntBase;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.db.sql.manager.DBOfflineManager;
import cn.lily.com.enviroment.Enviroment;
import cn.lily.com.lang.number.MathUtil;

public class YuiNyaNetPage extends NetAntBase {

	public YuiNyaNetPage(String netSite) {
		super(netSite);
	}
	
	public void analyse(){
		
		Elements activePart = this.document.select("section.container .content-wrap .content article.excerpt header h2 a");
		for(int i=0;i<activePart.size();i++){
			
			String hrefPage = activePart.get(i).attr("href");
			
			DataTable dataTable = this.getDBManager().executeQueryTable("select * from ${schema}.LEO_YUINYA_GAL_INFO where GAME_HREF='"+hrefPage+"'");
			if(dataTable.next()){
				continue;
			}
			
			try {
				
				Enviroment.getEnv().getLogBean().beginLog();
				
				YuiNyaNetEntity entity = new YuiNyaNetEntity();
				entity.setGameHref(hrefPage);
				
				YuiNyaNetDetail detail = new YuiNyaNetDetail(hrefPage);
				detail.setEntity(entity);
				detail.analyse();
				
				entity.insert();
				
				Enviroment.getEnv().getLogBean().endLog();
				
			} catch (Exception e) {
				
				e.printStackTrace();
				
				DBOfflineManager.getDBManager().executeUpdate("insert into ${schema}.LEO_YUINYA_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
						+ "values ('"+System.currentTimeMillis()+MathUtil.random(3)+"','"+hrefPage+"','2') ");
				
				continue;
				
			}
			
			System.out.println(activePart.get(i).attr("href"));
		}
		
	}

	public static void main(String[] args) {
		YuiNyaNetPage page = new YuiNyaNetPage("http://yui-nya.com/category/games/xz/page/2");
		page.analyse();
	}
	
}
